package com.example.hasee.zhcd.Base;

import android.app.Activity;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.view.View;

import com.example.hasee.zhcd.Application.AppManager;
import com.example.hasee.zhcd.Bean.EventMsg;
import com.example.hasee.zhcd.NetUtil.HttpUtils;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;


/*
* 基础类
* */
public abstract class BaseActivity extends Activity implements View.OnClickListener{

    //工具
    public HttpUtils httpUtils;
//    public AllUtils allUtils;
//    public LoadDialog loadDialog;

    //参数
    public String className; //类名
    public Activity activity;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);

        setContentView(contentViewId());
        AppManager.getAppManager().addActivity(this);//添加Activity到堆栈
        EventBus.getDefault().register(this); //注册EventBus
        httpUtils = HttpUtils.getInstance(this);//网络工具初始化
        className = getPackageName() + "." + getLocalClassName();
        activity = this;
        init();
        set();

    }


    @Override
    protected void onStart() {
        super.onStart();
    }

    @Override
    protected void onResume() {
        super.onResume();
    }

    @Override
    protected void onRestart() {
        super.onRestart();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
        //反注册EventBus
        AppManager.getAppManager().finishActivity(this);
        //结束Activity&从堆栈中移除
    }

    /**
     * 说明当前操作的layout的视图文件
     *
     * @return
     */
    public abstract int contentViewId();

    /**
     * 给页面组件添加数据
     */
    public abstract void set();

    /**
     * 初始化页面组件
     */
    public abstract void init();


    /**
     * 消息发到主线程ui界面
     */
    public abstract void getEventData(EventMsg msg);

    /**
     * eventbus 主线程回调
     */
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEventMainThread(EventMsg msg) {
        getEventData(msg);
    }


}
